<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>轮播图管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			checkSize();
			$("#inputForm").validate({
				submitHandler: function(form){
					var src = $("#pic").val();
					if(src == ""){
						$("#imgDiv").after("<label for='pic' id='errorLabel' style='margin-top:100px;' class='error'>请上传图片</label>")
					}else{
						loading('正在提交，请稍等...');
						form.submit();
					}
				},
				rules:{          
					remarks:{remarkCheck:true}
	            },
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			jQuery.validator.addMethod("remarkCheck", function(value,  
	                element, param) { 
				if(value.length>100){
					return false;
				} else{
					return true;
	            }
	        }, $.validator.format("字数超限"));
			$("#remarks").keyup(function(){
				checkSize();
			})
			$('#imgDiv').click(function(){
				$("#upload").trigger('click'); 
			})
		});
		function checkSize(){
			var curLength=$("#remarks").val().length;
			if(curLength>100){
				$("#textLength").css("color","red");
				$("#textLength").text(curLength+"/100");
			}else{
				$("#textLength").css("color","");
				$("#textLength").text(curLength+"/100");
			}
		}
		function showPic () {
			var r= new FileReader();
			f=document.getElementById('upload').files[0];
			r.readAsDataURL(f);
			r.onload=function  (e) {
				document.getElementById('picImg').src=this.result;
				document.getElementById('pic').value=this.result;
				var src = $("#pic").val();
				if(src != ""){
					$("#errorLabel").css("display","none");
				}
			};
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/sys/carousel/">轮播图列表</a></li>
		<li class="active"><a href="${ctx}/sys/carousel/form?id=${carousel.id}">轮播图<shiro:hasPermission name="sys:carousel:edit">${not empty carousel.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="sys:carousel:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" enctype="multipart/form-data" modelAttribute="carousel" action="${ctx}/sys/carousel/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<div style="display:none;">
			<sys:message content="${message}"/>
		</div>
		<div class="control-group">
			<label class="control-label">排序：</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" maxlength="11" class="input-xlarge digits"/>
			</div>
		</div>	
		<div class="control-group">
			<label class="control-label">链接：</label>
			<div class="controls">
				<form:input path="url" htmlEscape="false" maxlength="255" class="input-xlarge url"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">图片：</label>
			<div class="controls" style="width:200px;height:200px;float:left;margin-left:20px;cursor: pointer;padding:0;border: 2px solid rgba(0, 0, 0, 0.3);" id="imgDiv">
   				<img id="picImg" alt="点击上传图片" style="width:200px;height:200px;">
			</div>
			<input id="upload" name="file" type="file" style="display:none" onchange="showPic()""/>
			<input id="pic" name="pic" class="input-xlarge " type="hidden" maxlength="255">
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls" style="position: relative;">
				<span id="textLength" style="bottom:1px;position:absolute;line-height:1;margin-left: 475px;">0/100</span>
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>
		<div class="form-actions">
			<shiro:hasPermission name="sys:carousel:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>